﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Bon_It.Model
{
    using System.ComponentModel;

    using Bon_It.Model.Crypting;
    using Bon_It.Resources;

    public partial class Users:Entity<Users> 
    {

        /// <summary>
        /// Attach the property changed method from the base class.
        /// </summary>
        partial void OnCreated()
        {
            PropertyChanged += OnPropertyChanged;

        }

        /// <summary>
        /// When the <see cref="UserName"/> is changed. the password must be recreated.
        /// </summary>
        /// <param name="value"></param>
        partial void OnUserNameChanging(string value)
        {
            if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(Password))
            {
                // get the current cleartext password.
                string oldpassword = PasswordClear;

                // assign it with the new passphrase
                AssignNewPasswod(oldpassword, value);
            }

        }


        /// <summary>
        /// Gets or sets the cleartext password.
        /// </summary>
        /// <value>
        /// The password clear.
        /// </value>
        public string PasswordClear
        {
            get
            {
                if(string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
                {
                    Password = string.Empty;
                    return string.Empty;
                    
                }
                return Crypter.DecryptStringAES(Password,UserName);
            }
            set
            {
                // Only execute when username is set.
                if (!string.IsNullOrEmpty(UserName) && string.IsNullOrEmpty(value) ==false)
                {
                    
                    AssignNewPasswod(value, UserName);
                    this.OnPropertyChanged(this, new PropertyChangedEventArgs("PasswordClear"));
                }
            }
        }

        /// <summary>
        /// Assigns the new passwod.
        /// </summary>
        /// <param name="password">The password.</param>
        /// <param name="passphrase">The passphrase.</param>
        private void AssignNewPasswod(string password, string passphrase)
        {
              Password = Crypter.EncryptStringAES(password, passphrase);
                  
        }

        public override string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                    case "Password":
                    case "PasswordClear":
                        if (string.IsNullOrWhiteSpace(Password))
                        {
                            return ErrorResources.PasswordIsMissing;
                        }
                        break;
                  
                    case "UserName":
                        if (string.IsNullOrWhiteSpace(UserName))
                        {
                            return ErrorResources.UserNameIsMissing;
                        }
                        break;
                  case "LastName":
                        if (string.IsNullOrWhiteSpace(LastName))
                        {
                            return ErrorResources.LastNameIsMissing;
                        }
                        break;
                    case "UserGroup":
                        if (!UserGroup.HasValue)
                        {
                            return ErrorResources.UserGroupIsMissing;
                        }
                        break;
                }

                
                return base[columnName];
            }
        }
        
        public override string Error
        {
            get
            {
                if(!string.IsNullOrEmpty(this["UserName"])
                    || !string.IsNullOrEmpty(this["FirstName"])
                    || !string.IsNullOrEmpty(this["LastName"])
                    || !string.IsNullOrEmpty(this["UserGroup"])
                    )
                {
                    return "error";
                }
                return string.Empty;
            }
        }

    }
}
